package com.sharemedia.managers
{
	import com.sharemedia.consts.SortTrack;
	import com.sharemedia.vos.Track;
	import com.sharemedia.vos.TrackPrompt;
	
	import mx.collections.ArrayCollection;
	import mx.collections.ArrayList;
	import mx.collections.Sort;
	import mx.utils.ObjectUtil;

	public class TrackList
	{
		[Bindable]
		public var list:ArrayCollection;
		
		public function TrackList()
		{
			list = new ArrayCollection();
		}
		
		public function appendTracks(array:Array):void
		{
			list.addAll(new ArrayList(array));
		}
			
		public function addTrack(track:Track):void
		{
			list.addItem(track);
		}
		
		public function removeTrack(track:Track):void
		{
			var index:Number = list.getItemIndex(track);
			if(index != -1)
				list.removeItemAt(index);
		}
		
		private var _currentTrack:Track;

		public function get currentTrack():Track
		{
			if(!_currentTrack)
				_currentTrack = nextTrack();
			return _currentTrack;
		}

		public function set currentTrack(value:Track):void
		{
			_currentTrack = value;
		}

		private var _nextTrack:Track;
		
		public function setNextTrack(value:TrackPrompt):void
		{
			for each (var track:Track in list) 
			{
				if(track.uid == value.uid)
				{
					_nextTrack = track;
					return;
				}
			}
			
			_nextTrack = null;
		}
		
		public function nextTrack(shuffle:Boolean=false):Track
		{
			if(_nextTrack)
			{
				return _nextTrack;
				_nextTrack = null;
			}
			
			if(shuffle)
			{
				return getShuffledTrack();
			}
			else
			{
				var index:Number = list.getItemIndex(_currentTrack);
				index ++;
				
				if(index > list.length-1)
					index = 0;
				
				return list.getItemAt(index) as Track;
			}
		}
		
		public function getShuffledTrack():Track
		{
			var sTracks:Vector.<ShuffledTrack> = new Vector.<ShuffledTrack>();
			var pos:Number = 0;
			for each (var track:Track in list) 
			{
				var shuffledTrack:ShuffledTrack = new ShuffledTrack();
				shuffledTrack.track = track;
				shuffledTrack.rangeStart = pos;
				pos += track.rating > 0 ? track.rating : 5;
				shuffledTrack.rangeEnd = pos;
				
				sTracks.push(shuffledTrack);
			}
			
			var random:Number = Math.random()*pos;
			
			for each (var sTrack:ShuffledTrack in sTracks) 
			{
				if(random >= sTrack.rangeStart && random < sTrack.rangeEnd)
				{
					return  sTrack.track;
				}
			}
			return sTrack ? sTrack.track : null;
			
		}
		
		public function prevTrack():Track
		{
			var index:Number = list.getItemIndex(_currentTrack);
			
			index --;
			
			if(index < 0)
				index = 0;
			
			return list.getItemAt(index) as Track;
		}
		
		
		public function sort(sortBy:String, ascending:Boolean=true):void
		{
			var sort:Sort = new Sort();
			sort.compareFunction = sortFunc;
			
			if(sortBy == SortTrack.ORIGINAL)
				list.sort = null;
			else
				list.sort = sort;
			
			list.refresh();
			
			function sortFunc(a:Track, b:Track, fields:Array = null):Number
			{
				if(!ascending)
				{
					var buffer:Track = a;
					a = b;
					b = buffer;
				}
				
				var result:Number = 0;
				switch(sortBy)
				{
					case SortTrack.BY_NAME:
					{
						if (a.info == null && b.info == null)
							result = 0;
						else if (a.info == null)
							result = 1;
						else if (b.info == null)
							result = -1;
						else
							result = ObjectUtil.compare(a.info.name, b.info.name);
						
						break;
					}
					case SortTrack.BY_ARTIST:
					{
						if (a.info == null && b.info == null)
							result = 0;
						else if (a.info == null)
							result = 1;
						else if (b.info == null)
							result = -1;
						else
							result = ObjectUtil.compare(a.info.artist, b.info.artist);
						
						break;
					}
					case SortTrack.BY_LENGTH:
					{
						result = ObjectUtil.compare(a.length, b.length);
						break;
					}
					case SortTrack.BY_RATING:
					{
						result = ObjectUtil.compare(a.rating, b.rating);
						break;
					}
				}
				
				return result;
			}
		}
	}
}
import com.sharemedia.vos.Track;

class ShuffledTrack
{
	public var track:Track;
	public var rangeStart:Number;
	public var rangeEnd:Number;
}